import { http } from '@/utils/request'
import type { ApiResponse, PaginationResult } from '@/types'

/**
 * 积分系统API
 */
export const pointsApi = {
  // 获取用户积分信息
  getUserPoints(userId?: number): Promise<ApiResponse<{
    total_points: number
    available_points: number
    used_points: number
    level: number
    level_name: string
    next_level_points: number
    points_history: Array<{
      id: number
      type: string
      points: number
      description: string
      created_at: string
    }>
  }>> {
    const url = userId ? `/v1/users/${userId}/points` : '/v1/user/points'
    return http.get(url)
  },

  // 获取积分记录
  getPointsHistory(params: {
    page?: number
    page_size?: number
    type?: string
    start_date?: string
    end_date?: string
  } = {}): Promise<ApiResponse<PaginationResult<{
    id: number
    type: string
    points: number
    description: string
    source_type: string
    source_id: number
    created_at: string
  }>>> {
    return http.get('/v1/points/history', { params })
  },

  // 获取积分规则
  getPointsRules(): Promise<ApiResponse<Array<{
    id: number
    action: string
    points: number
    description: string
    is_active: boolean
  }>>> {
    return http.get('/v1/points/rules')
  },

  // 兑换积分
  redeemPoints(data: {
    item_id: number
    quantity: number
  }): Promise<ApiResponse<{
    success: boolean
    message: string
    remaining_points: number
  }>> {
    return http.post('/v1/points/redeem', data)
  },

  // 获取可兑换物品
  getRedeemableItems(params: {
    page?: number
    page_size?: number
    category?: string
  } = {}): Promise<ApiResponse<PaginationResult<{
    id: number
    name: string
    description: string
    points_cost: number
    category: string
    image_url?: string
    stock: number
    is_available: boolean
  }>>> {
    return http.get('/v1/points/items', { params })
  },

  // 获取积分排行榜
  getPointsLeaderboard(params: {
    page?: number
    page_size?: number
    period?: 'daily' | 'weekly' | 'monthly' | 'all'
  } = {}): Promise<ApiResponse<PaginationResult<{
    rank: number
    user_id: number
    username: string
    avatar_url?: string
    points: number
    level: number
    level_name: string
  }>>> {
    return http.get('/v1/points/leaderboard', { params })
  },

  // 获取用户等级信息
  getUserLevel(userId?: number): Promise<ApiResponse<{
    level: number
    level_name: string
    current_points: number
    next_level_points: number
    progress_percentage: number
    benefits: Array<{
      name: string
      description: string
    }>
  }>> {
    const url = userId ? `/v1/users/${userId}/level` : '/v1/user/level'
    return http.get(url)
  },

  // 获取积分统计
  getPointsStats(params: {
    period?: 'daily' | 'weekly' | 'monthly'
    start_date?: string
    end_date?: string
  } = {}): Promise<ApiResponse<{
    total_earned: number
    total_spent: number
    net_points: number
    daily_average: number
    top_actions: Array<{
      action: string
      count: number
      total_points: number
    }>
  }>> {
    return http.get('/v1/points/stats', { params })
  }
}
